//登录模块
define('login', ['config', 'jquery', 'cookie'], function(config, $, cookie){
	return function(){
		//$('#logModal .main-submit').click() 可能会导致error信息无法绑定事件
		$('#logModal .main-submit').on('click', function(){
			var account = $('#logAccount').val();
			var password = $('#logPassword').val();
			var flag = true;

			if (account == ''){
				$('#logModal .error:eq(0)').html('<span>账号不能为空</span>');
				flag = false;
			}
			if (password == ''){
				$('#logModal .error:eq(1)').html('<span>密码不能为空</span>')
				flag = false;
			}

			if (flag){	//信息格式正确，向服务器发送登录请求
				var request = $.ajax({
					url : config['api']['login'][0],
					type : config['api']['login'][1],
					data : {
						//将email修改为account
						account: account,
						password: password
					},
					dataType : 'json',
					async : true,
					error : function(err){
						console.log(err);
						if (err['status'] == '403'){
							$('#logModal .error:eq(1)').html('<span>账户或密码错误</span>');
						}
						else{
							$('#logModal .error:eq(1)').html('<span>连接服务器出错！</span>');
						}

						$('#logAccount').val('');
						$('#logPassword').val('');
						setTimeout(function(){
							$(document).one('click', function(){
								$('#logModal .error').html('');
							});
						}, 10);
					},
					success : function(data){
						console.log(data);
						//登录成功，将用户信息装入cookie
						cookie.setCookie('name', data['user']['name']);
						cookie.setCookie('userId', data['user']['userId']);
						cookie.setCookie('isTeacher', data['user']['isTeacher']);
						cookie.setCookie('avatarUrl', data['user']['avatarUrl']);
						cookie.setCookie('gender', data['user']['gender']);
						cookie.setCookie('grade', data['user']['grade']);
						cookie.setCookie('phone', data['user']['phone']);
						cookie.setCookie('province', data['user']['place']['province']);
						cookie.setCookie('city', data['user']['place']['city']);
						cookie.setCookie('school', data['user']['school']);
						//跳转到首页
						window.location.reload();
					}
				});
			}
			else{	//信息格式错误，绑定全局点击事件，去除错误提示信息
				//怪异bug，会多次点击，立即触发one事件。使用setTimeout解决
				setTimeout(function(){
					$(document).one('click', function(){
						$('#logModal .error').html('');
					});
				}, 10);
			}
		});
	};		
});

//微博与qq登录模块
define('other_log', ['cookie', 'jquery'], function(cookie, $){
	function handleData(data){	//<pre ... >...data...</pre>
		data = data.split('</')[0];
		data = data.split('>')[1];
		data = JSON.parse(data);

		if (!data['err']){
			cookie.setCookie('name', data['user']['name']);
			cookie.setCookie('userId', data['user']['userId']);
			cookie.setCookie('gender', data['user']['gender']);
			cookie.setCookie('phone', data['user']['phone']);
			cookie.setCookie('province', data['user']['place']['province']);
			cookie.setCookie('city', data['user']['place']['city']);
			cookie.setCookie('school', data['user']['school']);		
			window.location.reload();
		}
		else{
			alert('登录出错！请稍后重试');
			window.location.reload();; 
		}
	};

	return {
		weibo: function(){
			$(document).on('click', '.other_login img', function(){
				//https://api.weibo.com/oauth2/authorize?client_id=3394076065&redirect_uri=http://120.26.192.176/msq/api/user/weibo/auth
				var childWin = window.open('https://api.weibo.com/oauth2/authorize?client_id=3394076065&redirect_uri=http://120.26.192.176/msq/api/user/weibo/auth', 
					'微博登陆', 'height=500, width=500, toolbar=no, location=no, status=no');

				var cnt = 0;	//用于防止加载过慢时，setInterval多次调用成功情况函数
				var listenChildWin = setInterval(function(){
					var url = childWin.location.href;
					if (url == undefined){
						clearInterval(listenChildWin);
						return;
					}
					if (url.match(/http:\/\/120.26.192.176\/msq\/api\/user\/weibo/)){
						setTimeout(function(){
							if (childWin.document && cnt == 0){	//子window对象加载完毕
								cnt ++;
								clearInterval(listenChildWin);
								var data = childWin.document.body.innerHTML;
								childWin.close();	//关闭子窗口
								//解析服务器返回的字符串
								handleData(data);
							}	
							else{	//页面数据还没加载完毕，调用本身，30ms后再次尝试
								setTimeout(arguments.callee(), 50);
							}
						} , 50);
					}
				}, 200);
			});
		}
	};
});

